package cn.iocoder.yudao.module.energy.service.device.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.energy.controller.admin.device.vo.EnergyDeviceCreateReqVO;
import cn.iocoder.yudao.module.energy.controller.admin.device.vo.EnergyDevicePageReqVO;
import cn.iocoder.yudao.module.energy.controller.admin.device.vo.EnergyDeviceUpdateReqVO;
import cn.iocoder.yudao.module.energy.convert.device.EnergyDeviceConvert;
import cn.iocoder.yudao.module.energy.dal.dataobject.device.EnergyDeviceDO;
import cn.iocoder.yudao.module.energy.dal.mysql.device.EnergyDeviceMapper;
import cn.iocoder.yudao.module.energy.enums.device.DeviceStatusEnum;
import cn.iocoder.yudao.module.energy.service.device.EnergyDeviceService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.energy.enums.ErrorCodeConstants.ENERGY_DEVICE_NOT_EXISTS;

/**
 * 能耗设备 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class EnergyDeviceServiceImpl implements EnergyDeviceService {

    @Resource
    private EnergyDeviceMapper energyDeviceMapper;

    @Override
    public Long createEnergyDevice(EnergyDeviceCreateReqVO createReqVO) {
        // 插入
        EnergyDeviceDO energyDevice = EnergyDeviceConvert.INSTANCE.convert(createReqVO);
        // 默认状态为正常
        if (energyDevice.getStatus() == null) {
            energyDevice.setStatus(DeviceStatusEnum.NORMAL.getStatus());
        }
        energyDeviceMapper.insert(energyDevice);
        // 返回
        return energyDevice.getId();
    }

    @Override
    public void updateEnergyDevice(EnergyDeviceUpdateReqVO updateReqVO) {
        // 校验存在
        validateEnergyDeviceExists(updateReqVO.getId());
        // 更新
        EnergyDeviceDO updateObj = EnergyDeviceConvert.INSTANCE.convert(updateReqVO);
        energyDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteEnergyDevice(Long id) {
        // 校验存在
        validateEnergyDeviceExists(id);
        // 删除
        energyDeviceMapper.deleteById(id);
    }

    private void validateEnergyDeviceExists(Long id) {
        if (energyDeviceMapper.selectById(id) == null) {
            throw exception(ENERGY_DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public EnergyDeviceDO getEnergyDevice(Long id) {
        return energyDeviceMapper.selectById(id);
    }

    @Override
    public PageResult<EnergyDeviceDO> getEnergyDevicePage(EnergyDevicePageReqVO pageReqVO) {
        return energyDeviceMapper.selectPage(pageReqVO);
    }
    
    @Override
    public List<EnergyDeviceDO> getEnergyDeviceList() {
        return energyDeviceMapper.selectList();
    }
    
    @Override
    public List<EnergyDeviceDO> getEnergyDeviceList(EnergyDevicePageReqVO pageReqVO) {
        return energyDeviceMapper.selectList(pageReqVO);
    }
} 